package cn.stylefeng.guns.modular.rest.hxzhouzhuan;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.text.ParseException;

import cn.stylefeng.guns.modular.basic.model.params.BasicWarehouseParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicWarehouseResult;
import cn.stylefeng.guns.modular.basic.service.*;
import cn.stylefeng.guns.modular.hx.storage.entity.*;
import cn.stylefeng.guns.modular.hx.storage.model.params.*;
import cn.stylefeng.guns.modular.hx.storage.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 周转   管理
 *
 * @author zdw
 */
@Controller
@RequestMapping("/rest/hxzhouzhuan")
public class restHxTurnoverController extends BaseController {
    @Autowired
    private UserAop userAop;
    private Lock lock = new ReentrantLock();
    @Autowired
    private TurnoverContainerService turnoverContainerService;
    @Autowired
    private TurnoverContainerDetailedService turnoverContainerDetailedService;
    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @ResponseBody
    @RequestMapping("/turnoverContainerf")
    public ResponseData turnoverContainer( String[] packNumbers,TurnoverContainerDetailedParam turnoverContainerDetailedParam) throws ParseException {
        //获取操作人
        User user = userAop.getUser();
        BasicWarehouseParam wp = new BasicWarehouseParam();
        BasicWarehouseResult wr = basicWarehouseService.findBySpec(wp,user.getDeptId(),user.getOperateId());
        for (String packNumber : packNumbers) {
            Integer count = turnoverContainerService.lambdaQuery()
                    .eq(TurnoverContainer::getPackNumber, packNumber)
                    .count();
            if (count == 0) {
                return ResponseData.error("周转箱不存在");
            }else {
            if (turnoverContainerDetailedParam.getOperationType().equals("仓库出库")) {

                if (ToolUtil.isEmpty(turnoverContainerDetailedParam.getCustomerName())) {
                    return ResponseData.error("请填写门店名称");
                }
                if (ToolUtil.isEmpty(turnoverContainerDetailedParam.getDriverName())) {
                    return ResponseData.error("请填写司机姓名");
                }
                if (ToolUtil.isEmpty(turnoverContainerDetailedParam.getCarNumber())) {
                    return ResponseData.error("请填写司机车牌号");
                }
                if (ToolUtil.isEmpty(turnoverContainerDetailedParam.getTel())) {
                    return ResponseData.error("请填写司机手机号");
                }
                TurnoverContainerDetailedParam turnoverContainerDetailedParam1 = new TurnoverContainerDetailedParam();

                turnoverContainerDetailedParam1.setOperator(user.getName());

                turnoverContainerService.lambdaUpdate()
                        .eq(TurnoverContainer::getPackNumber, packNumber)
                        .set(TurnoverContainer::getStorageState, "在途")
                        .set(TurnoverContainer::getWarehouseName, wr.getWarehouseName())
                        .set(TurnoverContainer::getCustomerName, turnoverContainerDetailedParam.getCustomerName())
                        .set(TurnoverContainer::getOperator, user.getName())
                        .update();
                if (wr != null) {
                    turnoverContainerDetailedParam1.setWarehouseName(wr.getWarehouseName());
                    turnoverContainerDetailedParam1.setWarehouseId(wr.getId() + "");
                }
                turnoverContainerDetailedParam1.setPackNumber(packNumber);
                turnoverContainerDetailedParam1.setOperationType("仓库出库");
                turnoverContainerDetailedParam1.setCustomerName(turnoverContainerDetailedParam.getCustomerName());
                turnoverContainerDetailedParam1.setDeliverId(user.getDeliverId());
                turnoverContainerDetailedParam1.setSendTime(new Date());
                turnoverContainerDetailedParam1.setDriverName(turnoverContainerDetailedParam.getDriverName());
                turnoverContainerDetailedParam1.setCarNumber(turnoverContainerDetailedParam.getCarNumber());
                turnoverContainerDetailedParam1.setTel(turnoverContainerDetailedParam.getTel());
                this.turnoverContainerDetailedService.add(turnoverContainerDetailedParam1);
            }
            if (turnoverContainerDetailedParam.getOperationType().equals("门店出库")) {
                if (ToolUtil.isEmpty(turnoverContainerDetailedParam.getWarehouseName())) {
                    return ResponseData.error("请填写仓库名称");
                }
                if (ToolUtil.isEmpty(turnoverContainerDetailedParam.getDriverName())) {
                    return ResponseData.error("请填写司机姓名");
                }
                if (ToolUtil.isEmpty(turnoverContainerDetailedParam.getCarNumber())) {
                    return ResponseData.error("请填写司机车牌号");
                }
                if (ToolUtil.isEmpty(turnoverContainerDetailedParam.getTel())) {
                    return ResponseData.error("请填写司机手机号");
                }
                TurnoverContainerDetailedParam turnoverContainerDetailedParam2 = new TurnoverContainerDetailedParam();
                turnoverContainerService.lambdaUpdate()
                        .eq(TurnoverContainer::getPackNumber, packNumber)
                        .set(TurnoverContainer::getStorageState, "在途")
                        .set(TurnoverContainer::getWarehouseName, turnoverContainerDetailedParam.getWarehouseName())
                        .set(TurnoverContainer::getOperator, user.getName())
                        .update();
                turnoverContainerDetailedParam2.setCustomerName(user.getGetUnit());
                turnoverContainerDetailedParam2.setPackNumber(packNumber);
                turnoverContainerDetailedParam2.setOperationType("门店出库");
                turnoverContainerDetailedParam2.setWarehouseName(turnoverContainerDetailedParam.getWarehouseName());
                turnoverContainerDetailedParam2.setDeliverId(user.getDeliverId());
                turnoverContainerDetailedParam2.setSendTime(new Date());
                turnoverContainerDetailedParam2.setDriverName(turnoverContainerDetailedParam.getDriverName());
                turnoverContainerDetailedParam2.setCarNumber(turnoverContainerDetailedParam.getCarNumber());
                turnoverContainerDetailedParam2.setTel(turnoverContainerDetailedParam.getTel());
                turnoverContainerDetailedParam2.setCustomerName(user.getGetUnit());
                turnoverContainerDetailedParam2.setCustomerId(user.getGetUnitId());
                turnoverContainerDetailedParam2.setOperator(user.getName());
                this.turnoverContainerDetailedService.add(turnoverContainerDetailedParam2);
            }

        }
       }
        return ResponseData.success();
    }
    @ResponseBody
    @RequestMapping("/turnoverContainerss")
    public ResponseData TurnoverContainer(String[] packNumbers,String operationType,String attribute,String specifications) throws ParseException {
        //获取操作人
        User user = userAop.getUser();
        BasicWarehouseParam basicWarehouseParam = new BasicWarehouseParam();
        BasicWarehouseResult bySpec = basicWarehouseService.findBySpec(basicWarehouseParam, user.getDeptId(), user.getOperateId());
        for (String list2 : packNumbers) {
            Integer count = turnoverContainerService.lambdaQuery()
                    .eq(TurnoverContainer::getPackNumber, list2)
                    .count();
            if (count==0) {
                return ResponseData.error("周转箱不存在");
            }else{
            //根据操作人获取门店 或 仓库名  未完成
            if (operationType.equals("初始化")) {
                TurnoverContainerParam turnoverContainerParam = new TurnoverContainerParam();
                turnoverContainerParam.setOperator(user.getName());
                turnoverContainerParam.setPackNumber(list2);
                turnoverContainerParam.setStorageState("仓库");
                turnoverContainerParam.setAttribute(attribute);
                turnoverContainerParam.setSpecifications(specifications);
                turnoverContainerParam.setSendTime(new Date());
                if (bySpec != null) {
                    turnoverContainerParam.setWarehouseName(bySpec.getWarehouseName());
                    turnoverContainerParam.setWarehouseId((long) bySpec.getId());
                } else {
                    return ResponseData.error("请选择仓库");
                }
                turnoverContainerParam.setDeliverId(user.getDeliverId());
                turnoverContainerParam.setDeptId(user.getDeptId());
                turnoverContainerService.add(turnoverContainerParam);
            } else if (operationType.equals("门店入库")) {
                turnoverContainerService.lambdaUpdate()
                        .eq(TurnoverContainer::getPackNumber, list2)
                        .set(TurnoverContainer::getStorageState, "门店")
                        .set(TurnoverContainer::getCustomerName, user.getGetUnit())
                        .set(TurnoverContainer::getCustomerId, user.getGetUnitId())
                        .set(TurnoverContainer::getOperator, user.getName())
                        .update();
                TurnoverContainerDetailedParam turnoverContainerDetailedParam1 = new TurnoverContainerDetailedParam();
                //获取操作人
                turnoverContainerDetailedParam1.setOperator(user.getName());
                turnoverContainerDetailedParam1.setPackNumber(list2);
                turnoverContainerDetailedParam1.setOperationType("门店入库");
                turnoverContainerDetailedParam1.setCustomerId(user.getGetUnitId());
                turnoverContainerDetailedParam1.setCustomerName(user.getGetUnit());
                turnoverContainerDetailedParam1.setDeliverId(user.getDeliverId());
                turnoverContainerDetailedParam1.setSendTime(new Date());
                turnoverContainerDetailedService.add(turnoverContainerDetailedParam1);
            } else if (operationType.equals("仓库入库")) {
                turnoverContainerService.lambdaUpdate()
                        .eq(TurnoverContainer::getPackNumber, list2)
                        .set(TurnoverContainer::getStorageState, "仓库")
                        .set(TurnoverContainer::getWarehouseName, bySpec.getWarehouseName())
                        .set(TurnoverContainer::getWarehouseId, bySpec.getId())
                        .set(TurnoverContainer::getOperator, user.getName())
                        .update();
                TurnoverContainerDetailedParam turnoverContainerDetailedParam2 = new TurnoverContainerDetailedParam();
                //获取操作人
                turnoverContainerDetailedParam2.setOperator(user.getName());
                turnoverContainerDetailedParam2.setPackNumber(list2);
                turnoverContainerDetailedParam2.setOperationType("仓库入库");
                turnoverContainerDetailedParam2.setWarehouseId(bySpec.getId().toString());
                turnoverContainerDetailedParam2.setWarehouseName(bySpec.getWarehouseName());
                turnoverContainerDetailedParam2.setDeliverId(user.getDeliverId());
                turnoverContainerDetailedParam2.setSendTime(new Date());
                turnoverContainerDetailedService.add(turnoverContainerDetailedParam2);
            }
        }
        }
        return ResponseData.success();
    }
}